package com.dgut.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.admin.service.AuditService;
import com.dgut.common.entity.CollectorApplication;
import com.dgut.common.entity.UserInfo;
import com.dgut.common.enums.StatusEnum;
import com.dgut.common.mapper.CollectorApplicationMapper;
import com.dgut.common.mapper.UserInfoMapper;
import com.dgut.common.mapper.WxUserMapper;
import com.dgut.common.result.CollectorResult;
import com.dgut.common.result.UserInfoResult;
import com.dgut.common.utils.ApiException;
import com.dgut.common.utils.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author zyq
 * @Date 2022/4/9 21:53
 * @Description TODO
 * @Version 1.0
 **/
@Slf4j
@Service
public class AuditServiceImpl implements AuditService {
    private final UserInfoMapper infoMapper;
    private final CollectorApplicationMapper colMapper;
    private final WxUserMapper userMapper;

    public AuditServiceImpl(UserInfoMapper infoMapper, CollectorApplicationMapper colMapper, WxUserMapper userMapper) {
        this.infoMapper = infoMapper;
        this.colMapper = colMapper;
        this.userMapper = userMapper;
    }

    @Override
    public PageVO listUserInfo(Integer pageNum, Integer pageSize, String name, Integer state) {
        IPage<UserInfoResult> page = new Page<UserInfoResult>().setCurrent(pageNum).setSize(pageSize);
        Page<UserInfoResult> userInfoResultPage = infoMapper.selectMyPage(page, name, state);
        return PageVO.convert(userInfoResultPage);
    }

    @Override
    public void auditUserInfo(Integer id, Integer result) {
        if (!StatusEnum.SUCCESS_AUDIT.getState().equals(result) && !StatusEnum.FAIL_AUDIT.getState().equals(result)) {
            log.error("【AuditServiceImpl - auditUserInfo】The result is incorrect.");
            throw new ApiException("审核结果参数输错");
        }

        UserInfo userInfo = infoMapper.selectById(id);
        if (userInfo == null) {
            log.error("【AuditServiceImpl - auditUserInfo】UserInfo is null.");
            throw new ApiException("无法找到用户认证信息");
        }
        if (!StatusEnum.WAIT_AUDIT.getState().equals(userInfo.getState())) {
            log.error("【AuditServiceImpl - auditUserInfo】The state of userInfo is incorrect.");
            throw new ApiException("用户认证信息状态错误");
        }
        userInfo.setState(result);

        if (StatusEnum.SUCCESS_AUDIT.getState().equals(result)) {
            userMapper.updateIdentityById(StatusEnum.USER.getState(), userInfo.getUserId());
        }

        infoMapper.updateById(userInfo);
    }

    @Override
    public PageVO listCollector(Integer pageNum, Integer pageSize, String name, Integer state) {
        IPage<CollectorResult> page = new Page<CollectorResult>().setCurrent(pageNum).setSize(pageSize);
        Page<CollectorResult> collectorResultPage = infoMapper.selectCollectorPage(page, name, state);
        return PageVO.convert(collectorResultPage);
    }

    @Override
    @Transactional
    public void auditCollector(Integer id, Integer result) {
        if (!StatusEnum.SUCCESS_AUDIT.getState().equals(result) && !StatusEnum.FAIL_AUDIT.getState().equals(result)) {
            log.error("【AuditServiceImpl - auditCollector】The result is incorrect.");
            throw new ApiException("审核结果参数输错");
        }

        CollectorApplication application = colMapper.selectById(id);
        if (application == null) {
            log.error("【AuditServiceImpl - auditCollector】Application is null.");
            throw new ApiException("无法找到申请信息");
        }
        if (!StatusEnum.WAIT_AUDIT.getState().equals(application.getState())) {
            log.error("【AuditServiceImpl - auditCollector】The state of application is incorrect.");
            throw new ApiException("回收员资格申请状态错误");
        }
        application.setState(result);

        if (StatusEnum.SUCCESS_AUDIT.getState().equals(result)) {
            userMapper.updateIdentityById(StatusEnum.COLLECTOR.getState(), application.getUserId());
        }
        colMapper.updateById(application);
    }
}
